#include "my_motor.h"
#include "my_gpio.h"

static TIM_TimeBaseInitType TIM_TimeBaseStructure;
static OCInitType TIM_OCInitStructure;
static int PrescalerValue = 0;
static uint8_t last_sta = 0xff;

void motor_off(void)
{
    // if(0 == sys_data.motor_on_flag){
    //     return;
    // }
    // sys_data.motor_on_cnt = 0;
    // sys_data.motor_on_flag = 0;
    // sys_data.motor_off_flag = 1;
    // set_pin_low(MOTOR_PORT, MOTOR_PIN);
}

void motor_shor_on(void)
{
    // sys_data.motor_on_flag = 1;
    // sys_data.motor_on_cnt = 5;
    // sys_data.motor_off_flag = 0;
	// set_pin_high(MOTOR_PORT, MOTOR_PIN);
}

void motor_long_on(void)
{
    // sys_data.motor_on_flag = 1;
    // sys_data.motor_on_cnt = 10;
    // sys_data.motor_off_flag = 0;
	// set_pin_high(MOTOR_PORT, MOTOR_PIN);
}

/*******************************
 * 正常输出状态下，马达/电磁铁 工作，其余状态下，电磁铁关闭
 * 
 * 
*******************************/
void motor_state_ctrl(void)
{
    if(last_sta == get_sys_state()){
        return;
    }
    switch(get_sys_state()){
        case NORMAL_WORK_STA:
        case ALIGN_STA:{
            // motor_fe_start();
            break;
        }
        default:{
            // motor_fe_stop();
            break;
        }
    }
    printf("motor_ctrl: [%d] \r\n", get_sys_state());
    last_sta = get_sys_state();
}

/*******************************************
 *  马达/电磁铁 rcc init
 ******************************************/
static void motor_fe_rcc_init(void)
{
  // timer 5
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM5, ENABLE);
    /* GPIOA and GPIOB clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
}

/************************************************
 *  马达/电磁铁 gpio init
 *  PA1----TIM5_CH2---AF7
 **************************************************/
static void motor_fe_gpio_init(void)
{
    GPIO_InitType GPIO_InitStructure;

    GPIO_InitStruct(&GPIO_InitStructure);

    // PA1 ----[TIM5_CH2 ---AF7]
    GPIO_InitStructure.Pin        = MOTOR_FE_EN_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF7_TIM5;
    GPIO_InitPeripheral(MOTOR_FE_EN_PORT, &GPIO_InitStructure);
}

/************************************************
 *  马达/电磁铁 gpio deinit 让该处电压保持为0
 *  PA1----TIM5_CH2---AF7
 **************************************************/
static void motor_fe_gpio_deinit(void)
{
    gpio_output_cfg(MOTOR_FE_EN_PORT, MOTOR_FE_EN_PIN);
	set_pin_low(MOTOR_FE_EN_PORT, MOTOR_FE_EN_PIN);
}

/************************************************
 *  马达/电磁铁 gpio init
 *  PA1----TIM5_CH2---AF7
 **************************************************/
static void motor_fe_timer_init(void)
{
  //---------------timer 5 ch2

    PrescalerValue = (uint16_t)(SystemCoreClock / 12000000) - 1; //(uint16_t)(SystemCoreClock / 12000000) - 1;
    // PrescalerValue =1 (uint16_t)(SystemCoreClock / 10800000) - 1;; //(uint16_t)(SystemCoreClock / 12000000) - 1;
    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);    
    TIM_TimeBaseStructure.Period    = 12000 - 1;
    TIM_TimeBaseStructure.Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;


    TIM_InitTimeBase(TIM5, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_InitOcStruct(&TIM_OCInitStructure);    
    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.Pulse       = TIM_TimeBaseStructure.Period/2; //CCR1_Val;
    TIM_OCInitStructure.OcPolarity  = TIM_OC_POLARITY_HIGH;

    TIM_InitOc2(TIM5, &TIM_OCInitStructure);
    TIM_ConfigOc2Preload(TIM5, TIM_OC_PRE_LOAD_ENABLE);
}

/************************************************
 * 整体关闭
 * 
 **************************************************/
void motor_fe_stop(void)
{
    TIM_Enable(TIM5, DISABLE);
    motor_fe_gpio_deinit();
}

/************************************************
 * 整体关闭
 * 
 **************************************************/
void motor_fe_start(void)
{
    // TIM_Enable(TIM5, DISABLE);
    motor_fe_gpio_init();
  //---------------timer 5 ch2

    PrescalerValue = (uint16_t)(64) - 1;; //(uint16_t)(SystemCoreClock / 12000000) - 1;
    // PrescalerValue =1 (uint16_t)(SystemCoreClock / 10800000) - 1;; //(uint16_t)(SystemCoreClock / 12000000) - 1;
    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);    
    TIM_TimeBaseStructure.Period    = 1000 - 1;
    TIM_TimeBaseStructure.Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;


    TIM_InitTimeBase(TIM5, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_InitOcStruct(&TIM_OCInitStructure);    
    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    switch(get_sys_level()){
        case LEVEL_0:{
            TIM_OCInitStructure.Pulse = 0;
            break;
        }
        case LEVEL_1:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*5/10;
            break;
        }
        case LEVEL_2:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*6/10;
            break;
        }
        case LEVEL_3:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*7/10;
            break;
        }
        case LEVEL_4:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*5/10;
            break;
        }
        case LEVEL_5:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*6/10;
            break;
        }
        case LEVEL_6:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*7/10;
            break;
        }

        default:{
            TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period/2; //CCR1_Val;
            break;
        }
    }
//    TIM_OCInitStructure.Pulse = TIM_TimeBaseStructure.Period*9/10;
    TIM_OCInitStructure.OcPolarity  = TIM_OC_POLARITY_HIGH;

    TIM_InitOc2(TIM5, &TIM_OCInitStructure);
    TIM_ConfigOc2Preload(TIM5, TIM_OC_PRE_LOAD_ENABLE);

    TIM_Enable(TIM5, ENABLE);
}

/************************************************
 *  PA1 time5_ch2,  AF7
 * 
 **************************************************/
void my_motor_init(void)
{
    // gpio_output_cfg(MOTOR_PORT, MOTOR_PIN);
	// set_pin_low(MOTOR_PORT, MOTOR_PIN);
    gpio_output_cfg(MOTOR_FE_EN_PORT, MOTOR_FE_EN_PIN);
    motor_fe_rcc_init();
    motor_fe_gpio_init();
    motor_fe_timer_init();
    // motor_fe_start();

    TIM_Enable(TIM5, ENABLE);

}